import { computed } from 'vue'
import { useAuthStore } from '@/stores/auth'
import type { RouteLocationNormalized } from 'vue-router'

export function usePermission() {
  const authStore = useAuthStore()

  const hasPermission = (permission: string | string[]) => {
    if (!permission) return true
    
    const userPermissions = authStore.permissions
    if (Array.isArray(permission)) {
      return permission.some(p => userPermissions.includes(p))
    }
    return userPermissions.includes(permission)
  }

  const hasRole = (role: string | string[]) => {
    if (!role) return true
    
    const userRoles = authStore.roles
    if (Array.isArray(role)) {
      return role.some(r => userRoles.includes(r))
    }
    return userRoles.includes(role)
  }

  const accessRoute = (route: RouteLocationNormalized) => {
    const permission = route.meta?.permission
    const role = route.meta?.role
    
    return (
      !permission || hasPermission(permission as string | string[]) &&
      !role || hasRole(role as string | string[])
    )
  }

  return {
    hasPermission,
    hasRole,
    accessRoute
  }
} 